package com.david.mydemo.ctl_webservice.controller;

import com.david.mydemo.util.DateUtils;
import com.david.mydemo.util.XmlParserUtil;
import com.david.mydemo.ctl_webservice.WebServiceClient;
import com.david.mydemo.ctl_webservice.rsp.CheckTokenResponseInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;


@Controller
@RequestMapping("/ws")
@Api(value = "webservice示例", tags = "webservice示例")
@Slf4j
public class WsDemoController {

    //应用标识
    @Value("${ws.serviceId: GSNGGIS}")
    private String serviceId;
    //4A统一凭证校验WebService地址
    @Value("${ws.checkTokenWsUrl: http://10.251.22.163:20086/uac/services/CheckAiuapTokenSoap?wsdl}")
    private String checkTokenWsUrl;
    //4A统一凭证校验WebService方法
    @Value("${ws: CheckAiuapTokenSoap}")
    private String checkTokenWsMethod;

    /**
     * 4A跳转应用
     *
     * @param token     为4A系统生成的凭证串
     * @param appAcctId 为4A侧生成的代表应用从帐号的唯一标识，与应用侧的用户标识或登录名无关
     * @param flag      认证方式标识：统一凭证校验此值为1
     * @return
     */
    @SneakyThrows
    @GetMapping("/sys4a/ssoLogin")
    @ApiOperation(value = "4A跳转全业务一张图系统", notes = "4A跳转全业务一张图系统")
    public String ssoLogin4A(@RequestParam("token") String token, @RequestParam("appAcctId") String appAcctId, @RequestParam("flag") String flag, HttpServletRequest request) {
        // 4A统一凭证校验
        String xmlParam = "<soapenv:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:ser=\"http://service.base.app.core.a4.asiainfo.com\">" +
                "<soapenv:Header/>" +
                "<soapenv:Body>" +
                "<ser:CheckAiuapTokenSoap soapenv:encodingStyle=\"http://schemas.xmlsoap.org/soap/encoding/\">" +
                "<in0 xsi:type=\"soapenc:string\" xmlns:soapenc=\"http://schemas.xmlsoap.org/soap/encoding/\">" +
                "<![CDATA[" +
                "     <USERREQ>" +
                "     <HEAD>" +
                "     <CODE>000</CODE>" +
                "     <SID>000</SID>" +
                "     <TIMESTAMP>" + DateUtils.dateTimeNow() + "</TIMESTAMP>" +
                "     <SERVICEID>" + serviceId + "</SERVICEID>" +
                "     </HEAD>" +
                "     <BODY>" +
                "     <APPACCTID>" + appAcctId + "</APPACCTID>" +
                "     <TOKEN>" + token + "</TOKEN>" +
                "     </BODY>" +
                "     </USERREQ>" +
                "]]>" +
                "</in0>" +
                "</ser:CheckAiuapTokenSoap>" +
                "</soapenv:Body>" +
                "</soapenv:Envelope>";

        log.info("4A统一凭证校验参数：{}, WsUrl：{}，WsMethod: {}", xmlParam, checkTokenWsUrl, checkTokenWsMethod);
//        Object result = WebServiceClient.invokeWsApi(checkTokenWsUrl, checkTokenWsMethod, xmlParam);
        String result = WebServiceClient.invokeCheckAiuapTokenWsApi(checkTokenWsUrl, xmlParam);
        log.info("4A统一凭证校验返回截取后xml文本数据：{}", result);
        CheckTokenResponseInfo checkTokenResponseInfo = XmlParserUtil.parseFromXml(result, CheckTokenResponseInfo.class);
        if (checkTokenResponseInfo == null) {
            log.error("4A统一凭证校验返回数据转换失败");
            return "4A统一凭证校验返回数据转换失败";
        }
        if (!"0".equals(checkTokenResponseInfo.getCheckTokenRspBody().getRsp())) {
            log.error("4A统一凭证校验失败");
            return "4A统一凭证校验失败";
        }

        return "success";
    }

}
